using System;
using System.Linq;
using Coderoom.ScrumPlanner.Data.Repositories;
using Coderoom.ScrumPlanner.Domain;

namespace Coderoom.ScrumPlanner.Tests.FakeRepositories
{
	public sealed class FakeProjectRepository : ProjectRepository
	{
		private ProjectCollection _data;

		public FakeProjectRepository()
		{
			SetupFakeData();
		}

		public override ProjectCollection GetProjects()
		{
			return _data;
		}

		public override Project GetProject(Guid projectId)
		{
			return _data.Where(project => project.Id == projectId).SingleOrDefault();
		}

		public override Project GetProject(string slug)
		{
			return _data.Where(project => project.Slug == slug).SingleOrDefault();
		}

		public override void CreateProject(Project project)
		{
			project.Id = Guid.NewGuid();

			_data.Add(project);
		}

		public override bool DeleteProject(Guid projectId)
		{
			Project project;

			if ((project = GetProject(projectId)) != null)
				return _data.Remove(project);

			return false;
		}

		public override bool DeleteProject(string slug)
		{
			Project project;

			if ((project = GetProject(slug)) != null)
				return _data.Remove(project);

			return false;
		}

		private void SetupFakeData()
		{
			_data = new ProjectCollection();

			_data.Add(new Project {Id = new Guid("CF4F74C1-CE66-4B06-8480-B34445906423"), Title = "Project A"});
			_data.Add(new Project {Id = new Guid("D8185A6C-7145-4751-B149-27B03AB9E5B6"), Title = "Project B"});
			_data.Add(new Project {Id = new Guid("7E97578E-214E-4792-8B41-5D378E59AB2B"), Title = "Project X"});

			foreach (var project in _data)
				project.Slug = Entity.CreateSlug(project.Title);
		}
	}
}